package com.wkq.util.view

/**
 *
 *@Author: wkq
 *
 *@Time: 2025/7/24 15:27
 *
 *@Desc:
 */


import android.view.View
import androidx.viewpager2.widget.ViewPager2
import kotlin.math.abs
import kotlin.math.max

/**
 * ViewPager2 动画效果工具类
 * 支持多种切换效果，可直接设置到 ViewPager2 上
 */
object ViewPager2PageTransformerUtil {

    // 动画类型枚举
    enum class TransformerType {
        ZOOM_OUT,   // 缩小淡出
        DEPTH,      // 深度效果
        ROTATE,     // 旋转
        STACK,      // 堆叠
        PARALLAX,   // 视差
        FADE,       // 淡入淡出
        CUBE,       // 立方体旋转
        FLOW        // 流动翻页
    }

    /**
     * 应用指定的动画效果到 ViewPager2
     */
    fun applyTransformer(viewPager: ViewPager2, type: TransformerType) {
        viewPager.setPageTransformer(
            when (type) {
                TransformerType.ZOOM_OUT -> ZoomOutTransformer()
                TransformerType.DEPTH -> DepthTransformer()
                TransformerType.ROTATE -> RotateTransformer()
                TransformerType.STACK -> StackTransformer()
                TransformerType.PARALLAX -> ParallaxTransformer()
                TransformerType.FADE -> FadeTransformer()
                TransformerType.CUBE -> CubeTransformer()
                TransformerType.FLOW -> FlowTransformer()
            }
        )
    }

    /**
     * 缩小并淡出的动画
     */
    private class ZoomOutTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            val scale = max(0.85f, 1 - abs(position))
            val alpha = max(0.5f, 1 - abs(position))
            view.scaleX = scale
            view.scaleY = scale
            view.alpha = alpha
        }
    }

    /**
     * 深度缩放效果，进入页面放大显示，退出页面缩小淡出
     */
    private class DepthTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            when {
                position < -1f || position > 1f -> view.alpha = 0f
                position <= 0f -> {
                    view.alpha = 1f
                    view.translationX = 0f
                    view.scaleX = 1f
                    view.scaleY = 1f
                }
                else -> {
                    view.alpha = 1 - position
                    view.translationX = -view.width * position
                    val scale = 0.75f + (1 - 0.75f)*(1 - abs(position))
                    view.scaleX = scale
                    view.scaleY = scale
                }
            }
        }
    }

    /**
     * 页面旋转效果
     */
    private class RotateTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            view.pivotX = view.width * 0.5f
            view.pivotY = view.height.toFloat()
            view.rotation = 30f * position
        }
    }

    /**
     * 页面堆叠效果，带有缩放视觉
     */
    private class StackTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            view.translationX = -position * view.width
            view.scaleY = 1 - 0.1f * abs(position)
        }
    }

    /**
     * 视差动画（要求布局中有 id 为 parallaxView 的子 view）
     */
    private class ParallaxTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
//            val parallaxView = view.findViewById<View>(R.id.parallaxView)
//            parallaxView?.translationX = -position * (view.width / 2)
        }
    }

    /**
     * 淡入淡出效果
     */
    private class FadeTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            view.translationX = -position * view.width
            view.alpha = 1 - abs(position)
        }
    }

    /**
     * 立方体旋转效果
     */
    private class CubeTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            view.pivotY = (view.height / 2).toFloat()
            if (position < 0) {
                view.pivotX = view.width.toFloat()
                view.rotationY = 90f * abs(position)
            } else {
                view.pivotX = 0f
                view.rotationY = -90f * abs(position)
            }
        }
    }

    /**
     * 类似 Flow 效果（旋转 + 缩放）
     */
    private class FlowTransformer : ViewPager2.PageTransformer {
        override fun transformPage(view: View, position: Float) {
            view.rotationY = 30f * position
            val scale = max(0.7f, 1 - abs(position))
            view.scaleX = scale
            view.scaleY = scale
        }
    }
}
